//
//  BookPlaceDetailTravelController.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/28.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "BookPlaceDetailTravelController.h"
#import "BookPlaceTravelModel.h"
#import "PhotoViewController.h"
#import "BookPlaceDetailTravelCell.h"
#import "PhotosModel.h"
@interface BookPlaceDetailTravelController ()<BookPlaceTravelCollectionDelegate>

@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, assign) NSInteger moreNum;//加载更多
@end

static NSString *cellIdentifier = @"cell";
@implementation BookPlaceDetailTravelController
- (NSMutableArray *)modelArray
{
    if (!_modelArray)
    {
        self.modelArray = [NSMutableArray array];
    }
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.moreNum = 1;
    //http://chanyouji.com/api/attractions/35443.json?attraction_trips=true&page=1
    //self.url = http://chanyouji.com/api/attractions/%ld.json
    
    
    [self.tableView registerClass:[BookPlaceDetailTravelCell class] forCellReuseIdentifier:cellIdentifier];
    
    self.tableView.separatorStyle = NO;
    
    
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.moreNum = self.moreNum + 1;
        [self netRequestForBookPlaceDetailTravelWithUrl:[NSString stringWithFormat:@"%@?attraction_trips=ture&page=%ld", self.url,(long)self.moreNum]];
        [self.tableView.mj_footer endRefreshing];
    }];
    
    self.tableView.mj_footer = footer;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self netRequestForBookPlaceDetailTravelWithUrl:[NSString stringWithFormat:@"%@?attraction_trips=ture&page=%ld", self.url,(long)self.moreNum]];
        [self.tableView.mj_header endRefreshing];
    }];
    [self.tableView.mj_header beginRefreshing];
    

    
}

-(void)netRequestForBookPlaceDetailTravelWithUrl:(NSString *)url
{
   //判断网络状态;
    //[[NetworkState shareInstance] reachablityStates:self];
    [[NetworkState shareInstance]reachablityStatesWithNoJuhua:self];
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
         
          NSDictionary *zomDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in zomDic[@"attraction_trips"])
          {
              BookPlaceTravelModel *model = [[BookPlaceTravelModel alloc] init];
              model.nodesArray = [NSMutableArray array];
              [model setValuesForKeysWithDictionary:firDic];
              for (NSDictionary *secDic in firDic[@"notes"])
              {
                  PhotosModel *notModel = [[PhotosModel alloc] init];
                  [notModel setValuesForKeysWithDictionary:secDic];
                  if (notModel.image_url != nil)
                  {
                      [model.nodesArray addObject:notModel];
                  }
              }
              
              [_modelArray addObject:model];
          }
          dispatch_async(dispatch_get_main_queue(), ^{
              [self.tableView reloadData];
              // [MBProgressHUD hideHUDForView:self.view animated:YES];
          });
          
      }] resume];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BookPlaceDetailTravelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    BookPlaceTravelModel *model = self.modelArray[indexPath.row];

    cell.model = model;
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [BookPlaceDetailTravelCell heightForCell:self.modelArray[indexPath.row]];
}

#pragma mark -- collectionView跳转 --
- (void)JumpPageWithArray:(NSMutableArray *)array item:(NSInteger)item
{
    PhotoViewController *photoVc = [[PhotoViewController alloc]init];
    photoVc.modelArray = array;
    photoVc.item = item;
    [self presentViewController:photoVc animated:NO completion:nil];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
